package com.yc.sp_index.web;

import com.yc.common.bean.Result;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import com.yc.sp_index.web.fegin.ISp_file;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;


@RestController
@ConfigurationProperties(value = "myconfig.upload",
        ignoreInvalidFields = true)
public class VideoPlayerAction {

    private String uploadDir;
    private String webpath;

    //上传文件  废弃，不使用
    @PostMapping("upload0000000000000")
    public Result upload(@RequestParam(value = "savePath") String savePath,@RequestBody MultipartFile file) throws IOException {

        savePath=savePath.replace(webpath+"\\","");
        String filename = file.getOriginalFilename();
        //随机重命名
        String suffix = filename.replaceAll(".+(\\.\\w+)", "$1");//获取文件后缀

        filename = UUID.randomUUID().toString().replaceAll("-", "");
        filename += suffix;
        //构建本地路径
        String path=uploadDir+"/"+savePath+"/"+filename;
        path=path.replaceAll("/","\\\\");

        //保存文件
        file.transferTo(new File(path));
        //构建web路径
        String fileWebPath=webpath+"/"+savePath+"/"+filename;
        //返回
        return new Result(1,"文件上传成功", fileWebPath);
    }


    @Resource
    ISp_file iSp_file;

    @GetMapping("play{fid}")
    ModelAndView play(@RequestParam String fid, ModelAndView mav) {
        mav.setViewName("play");
        com.yc.cloudvideo.entity.File file=iSp_file.getFiles(fid);
        String path=file.getPath();
        String name=file.getFname();

        mav.addObject("path",path);
        mav.addObject("name",name);
        return mav;
    }



    public String getUploadDir() {
        return uploadDir;
    }

    public void setUploadDir(String uploadDir) {
        this.uploadDir = uploadDir;
    }

    public String getWebpath() {
        return webpath;
    }

    public void setWebpath(String webpath) {
        this.webpath = webpath;
    }
}
